export const  DMessageClient = class DMessageClient {
    constructor(httpServerAddr, httpProxyPath, messageHandler, roomName) {
        if (typeof SharedWorker === "undefined") {
            return null;
        } else {
            let jsPath = '/js/DMessageClientsSharedWorker.js?httpServerAddr=' + httpServerAddr + '&httpProxyPath=' + httpProxyPath
            if (process.env.NODE_ENV === 'production') {
                jsPath = '/qb' + jsPath
            }
            this.worker = new SharedWorker(jsPath);
            if (typeof messageHandler === 'function') {
                this.worker.port.addEventListener('message', messageHandler, false);
                this.worker.port.start();//open the port connection
                this.worker.port.postMessage({ eventName: "joinRoom", message: { roomName } });
                console.log(roomName);
            }
        }
    }
    /*
    *   sendMessage: 发送操作消息
    *   message是一个类：{message,currNo,groupNoInCourse,taskNoInGroup，guidelineNoInTask}
    *   类中的message是消息的内容，比如：ＸＸＸ开始学习任务ＸＸＸ；老师评阅了ＸＸＸ，９０分。
    *   其类的message、currNo是必须的，其余可选
    */
    sendMessage(message) {
        console.log('send', message);
        this.worker.port.postMessage({ eventName: "reportAction", message });
    }

    /*
    *  需要或不需要某个课程的消息
    *  默认是不需要（不发送消息过来） 
    *  发出去的操作消息，会发送给那些需要的网页
    */

    needNotMessageOf(roomName) {
        this.worker.port.postMessage({ eventName: "leaveRoom", message: { roomName } });
    }
    needMessageOf(roomName) {
        this.worker.port.postMessage({ eventName: "joinRoom", message: { roomName } });
    }

    closing() {
        this.worker.port.postMessage({ eventName: "portClosing", messge: {} })
    }

}